package com.quarantine.web.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.quarantine.model.applicationForMembership;
import com.quarantine.service.ApplicationForMembershipManager;
import com.quarantine.util.DateUtils;
import com.quarantine.web.CrudActionSupport;
@Namespace("/admin")
@Results({
	@Result(name ="save", location = "application-for-member-ship-admin!show.action", type ="redirect",params={"e","${e}"}),
})
public class ApplicationForMemberShipAdminAction extends CrudActionSupport<applicationForMembership> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private applicationForMembership entity;
	private ApplicationForMembershipManager application;
	private File pictruepath;
	private String pictruepathFileName;
	private String pictruepathContenttype;
	private String id;
	private String ids;// 逗号隔开id集合
	private String check;
	private int e;

	@Override
	public applicationForMembership getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		String root = ServletActionContext.getServletContext().getRealPath(
				"/upload");// 获取路径
		
	if(pictruepathFileName!=""&&pictruepathFileName!=null){            //有新文件上传时
		if (StringUtils.isEmpty(id)) {
               ;
		} else {
			entity = application.getById(id);
			File imgFile = new File(root + "/" + entity.getEnclosurepath());
			if (imgFile.exists()) {
				imgFile.delete();
			}
		}
		String fname = pictruepathFileName;  
		entity.setFilename(fname);                    // 重命名
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyyMMddHHmmss");
		java.util.Date mydate = new java.util.Date();
		fname = new String(sdf.format(mydate)
				+ fname.substring(fname.indexOf(".")));
		entity.setEnclosurepath(fname);                 //将新的文件名字戏写入entity中去 
		InputStream is = new FileInputStream(pictruepath);
		File destFile = new File(root, fname);
		OutputStream os = new FileOutputStream(destFile);
		byte[] buffer = new byte[1024];
		int length = 0;
		while (-1 != (length = is.read(buffer))) // 写入文件
		{
			os.write(buffer, 0, length);
		}
		is.close();
		os.close();
		
	}
	else{
		entity = application.getById(id);
	}
	    entity.setCheck1("1");
	    entity.setEditTime(DateUtils.now());
		application.set(entity); // 保存
        setE(1);
        Struts2Utils.renderText("r");
        return null;
	}
	 
	public String save1() throws Exception {
		entity.setId(id); 
		 System.out.println(entity.getIntroduction());
		 check=entity.getIntroduction();
		entity=application.getById(id);
		 System.out.println(entity.getIntroduction());
		entity.setIntroduction(check);
		 System.out.println(entity.getIntroduction());
	    entity.setCheck1("1");
	    entity.setEditTime(DateUtils.now());
		application.set(entity); // 保存
        setE(1);
        return "save";
	}
	
	
	public String show1() {
		setCheck("1");
		entity = application.getEntity(check);
		return "addapplication";
	}
	public String show() {
		setCheck("1");
		entity = application.getEntity(check);
		return "show";
	}

	@Override
	public String delete() throws Exception {
		entity = application.getById(id);
		String root = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		File imgFile = new File(root + "/" + entity.getEnclosurepath());
		if (imgFile.exists()) {
			imgFile.delete();
		}
		application.delete(id);
			return "delete";
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new applicationForMembership();
		} else {
			entity = application.getById(id);
		}
	}
		
	


	public applicationForMembership getEntity() {
		return entity;
	}

	public void setEntity(applicationForMembership entity) {
		this.entity = entity;
	}

	public File getPictruepath() {
		return pictruepath;
	}

	public void setPictruepath(File pictruepath) {
		this.pictruepath = pictruepath;
	}

	public String getPictruepathFileName() {
		return pictruepathFileName;
	}

	public void setPictruepathFileName(String pictruepathFileName) {
		this.pictruepathFileName = pictruepathFileName;
	}

	public String getPictruepathContenttype() {
		return pictruepathContenttype;
	}

	public void setPictruepathContenttype(String pictruepathContenttype) {
		this.pictruepathContenttype = pictruepathContenttype;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}


	public ApplicationForMembershipManager getApplication() {
		return application;
	}
    @Autowired
	public void setApplication(ApplicationForMembershipManager application) {
		this.application = application;
	}

	public String getCheck() {
		return check;
	}

	public void setCheck(String check) {
		this.check = check;
	}

	public int getE() {
		return e;
	}

	public void setE(int e) {
		this.e = e;
	}


}
